Completed
Push — master ( 401bbc...02c732 )
by Grant
05:46 queued 02:35
created

MobileMenu.checkWindowSize   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 1
rs 10
c 1
b 0
f 0
1
2
var MobileMenu = {};
3
4
MobileMenu.debug = true;
5
6
if (!window.console) {
7
    var console = {};
8
}
9
10
if (!console.log) {
0 ignored issues
show
Bug introduced by
The variable console does not seem to be initialized in case !window.console on line 6 is false. Are you sure this can never be the case?
Loading history...
11
    console.log = function () {};
12
}
13
14
// Window Event Listener =======================================================
15
16
MobileMenu.addWindowEventListener = function (event, myFunction) {
17
18
    if (window.addEventListener) {
19
20
        window.addEventListener(event, myFunction, false);
21
22
    } else if (window.attachEvent) {
23
24
        var onEvent = "on" + event;
25
26
        window.attachEvent(onEvent, myFunction);
27
28
    }
29
30
};
31
32
// Window Size Check for TabIndex ==============================================
33
34
MobileMenu.checkWindowSize = function() {
35
36
    var x = window.matchMedia("screen and (min-width: 1024px)");
37
38
    if (x.matches) {
39
40
        var mainMenu = document.getElementById("pageHeroNavigationMenu");
41
42
        var menuItems = mainMenu.querySelectorAll(".page-hero__navigation-item a");
43
44
        for (var i = 0; i < menuItems.length; i++) {
45
46
            menuItems[i].setAttribute("tabindex", "0");
47
48
        }
49
50
    }
51
52
}
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
53
54
MobileMenu.addWindowEventListener("load", MobileMenu.checkWindowSize);
55
56
// Mobile Navigation Triggers ==================================================
57
58
MobileMenu.mobileNavClickListener = function (e) {
59
60
    var mobileMenuTrigger = document.getElementById("pageHeroMobileTrigger");
0 ignored issues
show
Unused Code introduced by
The variable mobileMenuTrigger seems to be never used. Consider removing it.
Loading history...
61
62
    var mainMenu = document.getElementById("pageHeroNavigationMenu");
63
64
    var menuItems = mainMenu.querySelectorAll(".page-hero__navigation-item a");
65
66
    e.preventDefault();
67
68
    if (this.classList.contains("active")) {
69
70
        this.classList.remove("active");
71
72
        mainMenu.classList.remove("active");
73
74
        for (var i = 0; i < menuItems.length; i++) {
75
76
            menuItems[i].setAttribute("tabindex", "-1");
77
78
        }
79
80
        document.body.style.overflowY = "visible";
81
82
    } else {
83
84
        this.classList.add("active");
85
86
        mainMenu.classList.add("active");
87
88
        for (var i = 0; i < menuItems.length; i++) {
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable i already seems to be declared on line 74. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
Bug introduced by
It seems like i was already defined.
Loading history...
89
90
            menuItems[i].setAttribute("tabindex", "0");
91
92
        }
93
94
        document.body.style.overflowY = "hidden";
95
96
    }
97
98
};
99
100
MobileMenu.mobileNavKeyListener = function (e) {
101
102
    if (e.keyCode == 13) {
103
104
        this.click();
105
106
    }
107
108
};
109
110
MobileMenu.setMobileNavTriggers = function () {
111
112
    // Gets all elements on the page with "accordion-trigger".
113
    var mobileMenuTrigger = document.getElementById("pageHeroMobileTrigger");
114
115
    // Checks for a click.
116
    mobileMenuTrigger.addEventListener("click", MobileMenu.mobileNavClickListener);
117
118
    // Checks for an Enter key click.
119
    mobileMenuTrigger.addEventListener("keydown", MobileMenu.mobileNavKeyListener);
120
121
};
122
123
MobileMenu.addWindowEventListener("load", MobileMenu.setMobileNavTriggers);
124
125
// Mobile Navigation Tab Order =================================================
126
127
MobileMenu.mobileNavTabListener = function (e) {
128
129
    // Get the Main Menu
130
    var mainMenu = document.getElementById("pageHeroNavigationMenu");
131
132
    // Get Mobile Menu Trigger
133
    var menuTrigger = document.querySelector(".page-hero__mobile-trigger");
134
135
    // Get Mobile Menu Items
136
    var menuItems = mainMenu.querySelectorAll(".page-hero__navigation-item:not(.hidden) a");
137
138
    // Get Last Menu Item
139
    var lastMenuItem = menuItems[menuItems.length - 1]
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
140
141
    if (e.keyCode == 9) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if e.keyCode == 9 is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
142
143
        if (this === lastMenuItem) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if this === lastMenuItem is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
144
145
            e.preventDefault();
146
147
            menuTrigger.focus();
148
149
            return false;
150
151
        }
152
153
    }
154
155
}
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
156
157
MobileMenu.setMobileNavTabTriggers = function (e) {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
158
159
    var x = window.matchMedia("screen and (max-width: 1023px)");
160
161
    if (x.matches) {
162
163
        // Get the Main Menu
164
        var mainMenu = document.getElementById("pageHeroNavigationMenu");
165
166
        // Get Mobile Menu Items
167
        var menuItems = mainMenu.querySelectorAll(".page-hero__navigation-item a");
168
169
        // Set Triggers
170
        for (var i = 0; i < menuItems.length; i++) {
171
172
            menuItems[i].addEventListener("keydown", MobileMenu.mobileNavTabListener);
173
174
        }
175
176
    }
177
178
}
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
179
180
MobileMenu.addWindowEventListener("load", MobileMenu.setMobileNavTabTriggers);